<?php

namespace app\agent\controller;

class BaseSupplier extends Controller
{

    /**
     * 设置秘钥
     * @return \app\model\base\CommonModel|array|mixed|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function set()
    {

        $params = request()->all();
        if ($msg = dmjValidate([
            'code' => 'require',
            'serv_type' => 'require',
            'api_config' => 'require',
        ], [], request()->all())) {
            return fail($msg);
        }


        $agent_id = \app\common\facade\auth\Auth::id();
        $code = input('code');
        $serv_type = input('serv_type');

        $obj = \app\model\BaseSupplier::findOrCreate(compact('code','serv_type','agent_id'),$params);

        return success('',$obj);
    }


    /**
     * 获取供应商信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getByType()
    {

        $agent_id = \app\common\facade\auth\Auth::id();

        $code = input('code');
        if ($msg = dmjValidate([
            'code' => 'require'
        ], [], request()->all())) {
            return fail($msg);
        }

        $res = \app\model\BaseSupplier::query()
            ->where('agent_id', $agent_id)
            ->where('code', $code)
            ->find();

        return success('', $res);

    }

}